package io.codegeneratetool.web.controllers.opt.base;

import io.codegeneratetool.core.CodeGenerateOptions;
import io.codegeneratetool.core.FieldInfo;
import io.codegeneratetool.util.FieldUtil;
import io.codegeneratetool.web.entity.Setting;
import io.codegeneratetool.web.entity.SettingDetail;
import io.codegeneratetool.web.service.SettingDetailService;
import io.codegeneratetool.web.service.SettingService;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/opt/base/setting")
public class SettingController {
	@Autowired
	SettingService settingService;
	@Autowired
	SettingDetailService settingDetailService;
	
	@RequestMapping("index")
	public void index(){
		
	}
	@RequestMapping("addSetting")
	@ResponseBody
	public String addSetting(Setting setting){
		return ""+settingService.createOrUpdateEntity(setting);
		
	}
	
	@RequestMapping("findAllSetting")
	@ResponseBody
	public List<Setting> findAll(String dbconn_dbid){ 
		return settingService.findByFK(dbconn_dbid);
	}
	
	
	@RequestMapping("findAllSettingDetail")
	@ResponseBody
	public HashMap<String, Object> findAllSettingDetail(String setting_dbid){ 
		HashMap<String, Object> map=new HashMap<String, Object>(); 
		List<SettingDetail> findByFK = settingDetailService.findByFK(setting_dbid);
		if(findByFK!=null){
		for (SettingDetail detail : findByFK) {
			map.put(detail.getName(), detail.getValue()); 
		}
		}
		return map;
	}
	
	@RequestMapping("detailIndex") 
	public void detailIndex(String setting_dbid,Model model ){  
		CodeGenerateOptions t = new CodeGenerateOptions();
		List<FieldInfo> fields = FieldUtil.getFields(t, null);
		model.addAttribute("fields",fields);
		model.addAttribute("option",t);
	}
	
	@RequestMapping("addSettingDetails")
	@ResponseBody
	public String addSettingDetails(List<SettingDetail> details){
		for (SettingDetail settingDetail : details) {
			settingDetailService.createOrUpdateEntity(settingDetail);
		}
		return ""; 
	}
	
}
